<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function loadDDL(){
        $type=$_POST['Flag'];
        if($type=="ROLE"){
            $map['oi_name']='角色';
            $items=M('hs_code')->field('oi_code as id,oi_value as text')->where($map)->select();
            $this->ajaxreturn($items);
        }
        else if($type=="LX"){
            $map['oi_name']='类型';
            $items=M('hs_code')->field('oi_value as id,oi_value as text')->where($map)->select();
            $this->ajaxreturn($items);
        }else if($type=="WEIZHI"){
            $map['oi_name']='位置';
            $items=M('hs_code')->field('oi_value as id,oi_value as text')->where($map)->select();
            $this->ajaxreturn($items);
        }else if($type=="RY"){
            $map['oi_name']='人员';
            $items=M('hs_code')->field('oi_value as id,oi_value as text')->where($map)->select();
//          field第一个参数表示他给数据库传回的数据，第二个表示给用户展示的数据
            $this->ajaxreturn($items);
        }
        else if($type=="XMMC"){
            $items=M('hs_code')->distinct(true)->field('oi_name as id,oi_name as text')->select();
            $this->ajaxreturn($items);
        }
        else if($type=="PROVINCE"){
            $items=M()->table('province')->field('code as id,name as text')->order('px')->select();
            $this->ajaxreturn($items);
        }
        else if($type=="QYZT"){
            $map['oi_name']='企业状态';
            $items=M('hs_code')->field('oi_code as id,oi_value as text')->where($map)->select();
            $this->ajaxreturn($items);
        }
        else if($type=="XCRY"){
            $map['role']='02';
            $items=M('hs_user')->field('id,xm as text')->where($map)->select();
            $this->ajaxreturn($items);
        }
        else if($type=="QY"){
            $items=M('hs_company')->field('id,name as text')->select();
            $this->ajaxreturn($items);
        }
        else if($type=="ZT"){
            $map['oi_name']='状态';
            $items=M('hs_code')->field('oi_code as id,oi_value as text')->where($map)->select();
            $this->ajaxreturn($items);
        }
        else if($type=="LQR"){
            $map['role']='02';
            $items=M('hs_user')->field('id,xm as text')->where($map)->select();
            $this->ajaxreturn($items);
        }
        else if($type=="YJLB"){
            $items=M('hs_sjqd')->distinct(true)->field('lb as id,lb as text')->select();
            $this->ajaxreturn($items);
        }
    }
    //加载树形下拉列表 tree、combotree
    public function loadtree(){
        $type=$_POST['Flag'];
        if($type=='PART'){
            $moudule=M('hs_part');
            $items = $moudule->field('id,name as text,pid')->select();
            $tree = array(); //格式化的树
            $tmpMap = array(); //临时扁平数据
            $id='id';
            $pid='pid';
            $son = 'children';
            foreach ($items as $item) {
                $tmpMap[$item[$id]] = $item;
            }
            foreach ($items as $item) {
                if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
                } else {
                $tree[] = &$tmpMap[$item[$id]];
                }
            }
            unset($tmpMap);
            $this->ajaxreturn($tree);
        }
        else if($type=='COMPANY'){
            $moudule=M('hs_company');
            $items = $moudule->field('id,concat(gsmc,\'(\',gssx,\')\') as text,pid')->select();
            $tree = array(); //格式化的树
            $tmpMap = array(); //临时扁平数据
            $id='id';
            $pid='pid';
            $son = 'children';
            foreach ($items as $item) {
                $tmpMap[$item[$id]] = $item;
            }
            foreach ($items as $item) {
                if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
                } else {
                $tree[] = &$tmpMap[$item[$id]];
                }
            }
            unset($tmpMap);
            $this->ajaxreturn($tree);
        }
        else if($type=='ROLE'){
            $moudule=M('hs_role');
            $items = $moudule->field('id,name as text,pid')->select();
            $tree = array(); //格式化的树
            $tmpMap = array(); //临时扁平数据
            $id='id';
            $pid='pid';
            $son = 'children';
            foreach ($items as $item) {
                $tmpMap[$item[$id]] = $item;
            }
            foreach ($items as $item) {
                if (isset($tmpMap[$item[$pid]])) {
                $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
                } else {
                $tree[] = &$tmpMap[$item[$id]];
                }
            }
            unset($tmpMap);
            $this->ajaxreturn($tree);
        }
    }
    //城市
    public function getcity(){
        $code=$_REQUEST['code'];
        $map['provincecode']=$code;
        $items=M()->table('city')->field('code as id,name as text')->where($map)->select();
        $this->ajaxreturn($items);
    }
    //区域
    public function getarea(){
        $code=$_REQUEST['code'];
        $map['citycode']=$code;
        $items=M()->table('area')->field('code as id,name as text')->where($map)->select();
        $this->ajaxreturn($items);
    }
}